package com.yimi.yinhepda.views.stockCheck_module;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.DifferTaskAdapter;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.dao.DifferScanTableDao;
import com.yimi.yinhepda.entity.request.DifferReportRequest;
import com.yimi.yinhepda.entity.response.DifferTaskResponse;
import com.yimi.yinhepda.event.CountEvent;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.base.BaseInitActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 差异报列表, 根据网点获取数据deptCode
 */
public class DifferReportActivity extends BaseInitActivity {

    @Bind(R.id.title)
    TextView mTvTitle;//标题栏左边
    @Bind(R.id.num)
    TextView mTvNum;//标题栏中间
    @Bind(R.id.check_task_listview)
    ListView mListView;
    @Bind(R.id.edit_no)
    EditText mEditNo;
    @Bind(R.id.btn_check)
    Button mBtnCheck;

    private List<DifferTaskResponse.DifferTaskEntity> mDataList = new ArrayList<>();
    private DifferTaskAdapter mDifferTaskAdapter;

    //总的未上传数量
    private int totalNotUploadNum;
    private DifferScanTableDao mDifferScanTableDao;

    @Override
    public String getPageName() {
        return getString(R.string.page_differ_report);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_diff_report_list;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mTvTitle.setText("差异报告列表");
        EventBus.getDefault().register(this);
        //数据表
        mDifferScanTableDao = new DifferScanTableDao();
        //查询未上传数据
        totalNotUploadNum = mDifferScanTableDao.queryNotUploadDataNum();
        mTvNum.setText(totalNotUploadNum + "");
    }

    @Override
    public void initEvent() {
        findViewById(R.id.diff_report_btn_back).setOnClickListener(this);
        findViewById(R.id.diff_report_btn_refresh).setOnClickListener(this);
        findViewById(R.id.diff_report_btn_exec).setOnClickListener(this);
        mBtnCheck.setOnClickListener(this);

        mEditNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (mDataList.size() <= 0) {
                    showToast("暂无数据!");
                    return;
                }

                String inputStr = mEditNo.getText().toString().trim();
                List<DifferTaskResponse.DifferTaskEntity> mCheckList = new ArrayList<>();
                for (DifferTaskResponse.DifferTaskEntity item : mDataList) {
                    String differNo = item.getDiffreportNo();
                    if (differNo.startsWith(inputStr)) {
                        mCheckList.add(item);
                    }
                }
                mDifferTaskAdapter.setDataList(mCheckList);
                mDifferTaskAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    /**
     * 访问接口，获取数据
     */
    private void loadData() {
        showPgDlg("正在加载...");

        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(DifferTaskResponse.class)
                .setUrl(Constants.URL_DIFFER_TASK)
                .setRequestObject(new DifferReportRequest(SharedPreferUtil.getInstance().getValue(Common.DEPT_CODE)))
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        showToast(msg);
                        dismissPgDlg();
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        if (obj instanceof DifferTaskResponse) {
                            DifferTaskResponse response = (DifferTaskResponse) obj;
                            if (response.getData() == null || response.getData().size() == 0) {
                                showToast("暂时没有差异数据!");
                                if (mDataList != null) {
                                    mDataList.clear();
                                }
                                if (mDifferTaskAdapter != null) {
                                    mDifferTaskAdapter.setDataList(mDataList);
                                    mDifferTaskAdapter.notifyDataSetChanged();
                                }
                            } else {
                                showDataDetail(response);
                            }
                        }
                        dismissPgDlg();
                    }
                });
    }

    /**
     * 显示数据到列表
     */
    public void showDataDetail(DifferTaskResponse response) {

        mDataList = response.getData();
        //加载到列表
        mDifferTaskAdapter = new DifferTaskAdapter(this, mDataList);
        if (mListView != null) {
            mListView.setAdapter(mDifferTaskAdapter);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.diff_report_btn_back:
                finish();
                break;
            case R.id.diff_report_btn_refresh:
                loadData();
                break;
            case R.id.diff_report_btn_exec:
                if (mDifferTaskAdapter == null) {
                    showToast("暂时没有数据!");
                    return;
                }
                int i = mDifferTaskAdapter.getCurrentPosition();
                if (i != -1) {
                    //获取差异任务号
                    String taskCode = mDataList.get(i).getDiffreportNo();
                    Intent intent = new Intent(DifferReportActivity.this, DifferScanActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("taskCode", taskCode);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    showToast("请选择一项任务执行!");
                }
                break;
            case R.id.btn_check:
                //查询
                startCheckData();
                break;
            default:
                break;
        }
    }

    /**
     * 查询数据
     */
    private void startCheckData() {
        String inputNo = mEditNo.getText().toString().trim();
        if (TextUtils.isEmpty(inputNo)) {
            showToast("请输入编号");
            return;
        }
        if (mDataList.size() <= 0 || mDifferTaskAdapter == null) {
            showToast("暂时无数据");
            return;
        }
        List<DifferTaskResponse.DifferTaskEntity> mCheckList = new ArrayList<>();
        for (DifferTaskResponse.DifferTaskEntity item : mDataList) {
            String differNo = item.getDiffreportNo();
            if (differNo.startsWith(inputNo)) {
                mCheckList.add(item);
            }
        }
        mDifferTaskAdapter.setDataList(mCheckList);
        mDifferTaskAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainEventBus(CountEvent countEvent) {
        Log.e("", "event---->" + countEvent.getCount());
        mTvNum.setText(countEvent.getCount() + "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
